/*
 * NoticeView.java
 */

package notice;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import bacom.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import javax.swing.SwingWorker;

/**
 * The application's main frame.
 */
public class NoticeView extends FrameView {

    public NoticeView(SingleFrameApplication app) {
        super(app);

        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);

    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = Notice.getApplication().getMainFrame();
            aboutBox = new NoticeAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        Notice.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jComboBoxChipType = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldNormalCelFile1 = new javax.swing.JTextField();
        jLabelNormalCel1 = new javax.swing.JLabel();
        jTextFieldNormalCelFile2 = new javax.swing.JTextField();
        jLabelNormalCel2 = new javax.swing.JLabel();
        jTextFieldTumorCelFile1 = new javax.swing.JTextField();
        jLabelTumorCel1 = new javax.swing.JLabel();
        jLabelTumorCel2 = new javax.swing.JLabel();
        jTextFieldTumorCelFile2 = new javax.swing.JTextField();
        jButtonNormalCelFileChooser1 = new javax.swing.JButton();
        jButtonNormalCelFileChooser2 = new javax.swing.JButton();
        jButtonTumorCelFileChooser2 = new javax.swing.JButton();
        jButtonTumorCelFileChooser1 = new javax.swing.JButton();
        jTextFieldCdfFile1 = new javax.swing.JTextField();
        jButtonCdfFileChooser1 = new javax.swing.JButton();
        jButtonCdfFileChooser2 = new javax.swing.JButton();
        jLabelCdf2 = new javax.swing.JLabel();
        jTextFieldCdfFile2 = new javax.swing.JTextField();
        jLabelCdf1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaResult = new javax.swing.JTextArea();
        jButtonStartAnalysis = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        jFileChooserCelFile = new javax.swing.JFileChooser();
        jFileChooserCdfFile = new javax.swing.JFileChooser();

        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setPreferredSize(new java.awt.Dimension(850, 700));
        mainPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(notice.Notice.class).getContext().getResourceMap(NoticeView.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        mainPanel.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 47, 238, -1));

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        mainPanel.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 47, -1, -1));

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N
        mainPanel.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 159, -1, -1));

        jComboBoxChipType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Please select ...", "Affymetrix GeneChip Human Mapping 250K Nsp Array", "Affymetrix GeneChip Human Mapping 250K Sty Set", "Affymetrix GeneChip Human Mapping 500K Array Set", "Affymetrix Genome-Wide Human SNP Array 6.0" }));
        jComboBoxChipType.setName("jComboBoxChipType"); // NOI18N
        jComboBoxChipType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxChipTypeActionPerformed(evt);
            }
        });
        mainPanel.add(jComboBoxChipType, new org.netbeans.lib.awtextra.AbsoluteConstraints(92, 14, -1, -1));

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N
        mainPanel.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 19, -1, -1));

        jTextFieldNormalCelFile1.setEditable(false);
        jTextFieldNormalCelFile1.setText(resourceMap.getString("jTextFieldNormalCelFile1.text")); // NOI18N
        jTextFieldNormalCelFile1.setEnabled(false);
        jTextFieldNormalCelFile1.setName("jTextFieldNormalCelFile1"); // NOI18N
        jTextFieldNormalCelFile1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextFieldNormalCelFile1MouseClicked(evt);
            }
        });
        mainPanel.add(jTextFieldNormalCelFile1, new org.netbeans.lib.awtextra.AbsoluteConstraints(88, 72, 296, -1));

        jLabelNormalCel1.setText(resourceMap.getString("jLabelNormalCel1.text")); // NOI18N
        jLabelNormalCel1.setName("jLabelNormalCel1"); // NOI18N
        mainPanel.add(jLabelNormalCel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 70, -1, 30));

        jTextFieldNormalCelFile2.setEditable(false);
        jTextFieldNormalCelFile2.setText(resourceMap.getString("jTextFieldNormalCelFile2.text")); // NOI18N
        jTextFieldNormalCelFile2.setEnabled(false);
        jTextFieldNormalCelFile2.setName("jTextFieldNormalCelFile2"); // NOI18N
        jTextFieldNormalCelFile2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextFieldNormalCelFile2MouseClicked(evt);
            }
        });
        mainPanel.add(jTextFieldNormalCelFile2, new org.netbeans.lib.awtextra.AbsoluteConstraints(88, 119, 296, -1));

        jLabelNormalCel2.setText(resourceMap.getString("jLabelNormalCel2.text")); // NOI18N
        jLabelNormalCel2.setName("jLabelNormalCel2"); // NOI18N
        mainPanel.add(jLabelNormalCel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 124, -1, -1));

        jTextFieldTumorCelFile1.setEditable(false);
        jTextFieldTumorCelFile1.setEnabled(false);
        jTextFieldTumorCelFile1.setName("jTextFieldTumorCelFile1"); // NOI18N
        jTextFieldTumorCelFile1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextFieldTumorCelFile1MouseClicked(evt);
            }
        });
        mainPanel.add(jTextFieldTumorCelFile1, new org.netbeans.lib.awtextra.AbsoluteConstraints(552, 70, 296, -1));

        jLabelTumorCel1.setText(resourceMap.getString("jLabelTumorCel1.text")); // NOI18N
        jLabelTumorCel1.setName("jLabelTumorCel1"); // NOI18N
        mainPanel.add(jLabelTumorCel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 70, 60, 27));

        jLabelTumorCel2.setText(resourceMap.getString("jLabelTumorCel2.text")); // NOI18N
        jLabelTumorCel2.setName("jLabelTumorCel2"); // NOI18N
        mainPanel.add(jLabelTumorCel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 120, -1, -1));

        jTextFieldTumorCelFile2.setEditable(false);
        jTextFieldTumorCelFile2.setEnabled(false);
        jTextFieldTumorCelFile2.setName("jTextFieldTumorCelFile2"); // NOI18N
        jTextFieldTumorCelFile2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextFieldTumorCelFile2MouseClicked(evt);
            }
        });
        mainPanel.add(jTextFieldTumorCelFile2, new org.netbeans.lib.awtextra.AbsoluteConstraints(552, 115, 296, -1));

        jButtonNormalCelFileChooser1.setText(resourceMap.getString("jButtonNormalCelFileChooser1.text")); // NOI18N
        jButtonNormalCelFileChooser1.setEnabled(false);
        jButtonNormalCelFileChooser1.setName("jButtonNormalCelFileChooser1"); // NOI18N
        jButtonNormalCelFileChooser1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNormalCelFileChooser1ActionPerformed(evt);
            }
        });
        mainPanel.add(jButtonNormalCelFileChooser1, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 71, -1, -1));

        jButtonNormalCelFileChooser2.setText(resourceMap.getString("jButtonNormalCelFileChooser2.text")); // NOI18N
        jButtonNormalCelFileChooser2.setEnabled(false);
        jButtonNormalCelFileChooser2.setName("jButtonNormalCelFileChooser2"); // NOI18N
        jButtonNormalCelFileChooser2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNormalCelFileChooser2ActionPerformed(evt);
            }
        });
        mainPanel.add(jButtonNormalCelFileChooser2, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 118, -1, -1));

        jButtonTumorCelFileChooser2.setText(resourceMap.getString("jButtonTumorCelFileChooser2.text")); // NOI18N
        jButtonTumorCelFileChooser2.setEnabled(false);
        jButtonTumorCelFileChooser2.setName("jButtonTumorCelFileChooser2"); // NOI18N
        jButtonTumorCelFileChooser2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonTumorCelFileChooser2ActionPerformed(evt);
            }
        });
        mainPanel.add(jButtonTumorCelFileChooser2, new org.netbeans.lib.awtextra.AbsoluteConstraints(854, 117, -1, -1));

        jButtonTumorCelFileChooser1.setText(resourceMap.getString("jButtonTumorCelFileChooser1.text")); // NOI18N
        jButtonTumorCelFileChooser1.setEnabled(false);
        jButtonTumorCelFileChooser1.setName("jButtonTumorCelFileChooser1"); // NOI18N
        jButtonTumorCelFileChooser1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonTumorCelFileChooser1ActionPerformed(evt);
            }
        });
        mainPanel.add(jButtonTumorCelFileChooser1, new org.netbeans.lib.awtextra.AbsoluteConstraints(854, 70, -1, -1));

        jTextFieldCdfFile1.setEditable(false);
        jTextFieldCdfFile1.setEnabled(false);
        jTextFieldCdfFile1.setName("jTextFieldCdfFile1"); // NOI18N
        jTextFieldCdfFile1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextFieldCdfFile1MouseClicked(evt);
            }
        });
        mainPanel.add(jTextFieldCdfFile1, new org.netbeans.lib.awtextra.AbsoluteConstraints(88, 186, 296, -1));

        jButtonCdfFileChooser1.setText(resourceMap.getString("jButtonCdfFileChooser1.text")); // NOI18N
        jButtonCdfFileChooser1.setEnabled(false);
        jButtonCdfFileChooser1.setName("jButtonCdfFileChooser1"); // NOI18N
        jButtonCdfFileChooser1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCdfFileChooser1ActionPerformed(evt);
            }
        });
        mainPanel.add(jButtonCdfFileChooser1, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 185, -1, -1));

        jButtonCdfFileChooser2.setText(resourceMap.getString("jButtonCdfFileChooser2.text")); // NOI18N
        jButtonCdfFileChooser2.setEnabled(false);
        jButtonCdfFileChooser2.setName("jButtonCdfFileChooser2"); // NOI18N
        jButtonCdfFileChooser2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCdfFileChooser2ActionPerformed(evt);
            }
        });
        mainPanel.add(jButtonCdfFileChooser2, new org.netbeans.lib.awtextra.AbsoluteConstraints(854, 185, -1, -1));

        jLabelCdf2.setText(resourceMap.getString("jLabelCdf2.text")); // NOI18N
        jLabelCdf2.setName("jLabelCdf2"); // NOI18N
        mainPanel.add(jLabelCdf2, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 191, -1, -1));

        jTextFieldCdfFile2.setEditable(false);
        jTextFieldCdfFile2.setEnabled(false);
        jTextFieldCdfFile2.setName("jTextFieldCdfFile2"); // NOI18N
        jTextFieldCdfFile2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextFieldCdfFile2MouseClicked(evt);
            }
        });
        mainPanel.add(jTextFieldCdfFile2, new org.netbeans.lib.awtextra.AbsoluteConstraints(552, 186, 296, -1));

        jLabelCdf1.setText(resourceMap.getString("jLabelCdf1.text")); // NOI18N
        jLabelCdf1.setName("jLabelCdf1"); // NOI18N
        mainPanel.add(jLabelCdf1, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 184, -1, 30));

        jSeparator1.setName("jSeparator1"); // NOI18N
        mainPanel.add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 279, 921, 22));

        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator2.setName("jSeparator2"); // NOI18N
        mainPanel.add(jSeparator2, new org.netbeans.lib.awtextra.AbsoluteConstraints(446, 70, 22, 76));

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextAreaResult.setColumns(20);
        jTextAreaResult.setLineWrap(true);
        jTextAreaResult.setRows(5);
        jTextAreaResult.setAutoscrolls(true);
        jTextAreaResult.setMargin(new java.awt.Insets(6, 6, 6, 6));
        jTextAreaResult.setName("jTextAreaResult"); // NOI18N
        jTextAreaResult.setPreferredSize(new java.awt.Dimension(250, 60));
        jScrollPane1.setViewportView(jTextAreaResult);

        mainPanel.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(62, 300, 810, 420));

        jButtonStartAnalysis.setText(resourceMap.getString("jButtonStartAnalysis.text")); // NOI18N
        jButtonStartAnalysis.setEnabled(false);
        jButtonStartAnalysis.setName("jButtonStartAnalysis"); // NOI18N
        jButtonStartAnalysis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonStartAnalysisActionPerformed(evt);
            }
        });
        mainPanel.add(jButtonStartAnalysis, new org.netbeans.lib.awtextra.AbsoluteConstraints(379, 235, 189, -1));

        jLabel4.setIcon(resourceMap.getIcon("jLabel4.icon")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N
        mainPanel.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(841, 12, -1, -1));

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(notice.Notice.class).getContext().getActionMap(NoticeView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 933, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 909, Short.MAX_VALUE)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel))
                .addGap(3, 3, 3))
        );

        jFileChooserCelFile.setName("jFileChooserCelFile"); // NOI18N

        jFileChooserCdfFile.setName("jFileChooserCdfFile"); // NOI18N

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBoxChipTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxChipTypeActionPerformed
        // TODO add your handling code here:
        JComboBox cb = (JComboBox)evt.getSource();
        chipType = (String)cb.getSelectedItem();
        if (chipType.equals("Affymetrix GeneChip Human Mapping 500K Array Set")) {
            jLabelNormalCel1.setText("Nsp chip");
            jLabelNormalCel2.setText("Sty chip");
            jLabelTumorCel1.setText("Nsp chip");
            jLabelTumorCel2.setText("Sty chip");

            jTextFieldNormalCelFile1.setEnabled(true);
            jTextFieldNormalCelFile2.setEnabled(true);
            jTextFieldTumorCelFile1.setEnabled(true);
            jTextFieldTumorCelFile2.setEnabled(true);

            jTextFieldNormalCelFile1.setText("");
            jTextFieldNormalCelFile2.setText("");
            jTextFieldTumorCelFile1.setText("");
            jTextFieldTumorCelFile2.setText("");


            jButtonNormalCelFileChooser1.setEnabled(true);
            jButtonNormalCelFileChooser2.setEnabled(true);
            jButtonTumorCelFileChooser1.setEnabled(true);
            jButtonTumorCelFileChooser2.setEnabled(true);

            jLabelCdf1.setText("Nsp chip");
            jLabelCdf2.setText("Sty chip");
            jButtonCdfFileChooser1.setEnabled(true);
            jButtonCdfFileChooser2.setEnabled(true);
            jTextFieldCdfFile1.setEnabled(true);
            jTextFieldCdfFile2.setEnabled(true);

            jTextFieldCdfFile1.setText("");
            jTextFieldCdfFile2.setText("");



        } else {
            jLabelNormalCel1.setText("CEL");
            jLabelNormalCel2.setText("CEL");
            jLabelTumorCel1.setText("CEL");
            jLabelTumorCel2.setText("CEL");

            jLabelCdf1.setText("CDF");
            jLabelCdf2.setText("CDF");

            jButtonCdfFileChooser1.setEnabled(true);
            jButtonCdfFileChooser2.setEnabled(false);
            jTextFieldCdfFile1.setEnabled(true);
            jTextFieldCdfFile2.setEnabled(false);
            jTextFieldNormalCelFile1.setText("");
            jTextFieldNormalCelFile2.setText("");
            jTextFieldTumorCelFile1.setText("");
            jTextFieldTumorCelFile2.setText("");

            jTextFieldNormalCelFile1.setEnabled(true);
            jTextFieldNormalCelFile2.setEnabled(false);
            jTextFieldTumorCelFile1.setEnabled(true);
            jTextFieldTumorCelFile2.setEnabled(false);
            jTextFieldCdfFile1.setText("");
            jTextFieldCdfFile2.setText("");

            jButtonNormalCelFileChooser1.setEnabled(true);
            jButtonNormalCelFileChooser2.setEnabled(false);
            jButtonTumorCelFileChooser1.setEnabled(true);
            jButtonTumorCelFileChooser2.setEnabled(false);

        }

        normalNspCelFileName = null;
        tumorNspCelFileName = null;
        nspCdfFileName = null;
        normalStyCelFileName = null;
        tumorStyCelFileName = null;
        styCdfFileName = null;
        normalCelFileName = null;
        tumorCelFileName = null;
        cdfFileName = null;          


    }//GEN-LAST:event_jComboBoxChipTypeActionPerformed

    private void jButtonNormalCelFileChooser1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNormalCelFileChooser1ActionPerformed
        // TODO add your handling code here:
        int returnVal = jFileChooserCelFile.showOpenDialog(mainPanel);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = jFileChooserCelFile.getSelectedFile();
                //This is where a real application would save the file.
                jTextFieldNormalCelFile1.setText(jFileChooserCelFile.getName(file));
                if (chipType.equals("Affymetrix GeneChip Human Mapping 500K Array Set")) {
                    normalNspCelFileName = file.getPath();
                } else {
                    normalCelFileName = file.getPath();
                }

            } else {

            }

        if ((normalCelFileName != null && tumorCelFileName != null && cdfFileName != null) ||
                (normalNspCelFileName != null && tumorNspCelFileName != null && nspCdfFileName != null
                && normalStyCelFileName != null && tumorStyCelFileName != null
                && styCdfFileName != null)) {
                jButtonStartAnalysis.setEnabled(true);

        }
        
    }//GEN-LAST:event_jButtonNormalCelFileChooser1ActionPerformed

    private void jButtonTumorCelFileChooser1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonTumorCelFileChooser1ActionPerformed
        // TODO add your handling code here:
        int returnVal = jFileChooserCelFile.showOpenDialog(mainPanel);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = jFileChooserCelFile.getSelectedFile();
                //This is where a real application would save the file.
                jTextFieldTumorCelFile1.setText(jFileChooserCelFile.getName(file));
                if (chipType.equals("Affymetrix GeneChip Human Mapping 500K Array Set")) {
                    tumorNspCelFileName = file.getPath();
                } else {
                    tumorCelFileName = file.getPath();
                }

            } else {

            }


        if ((normalCelFileName != null && tumorCelFileName != null && cdfFileName != null) ||
                (normalNspCelFileName != null && tumorNspCelFileName != null && nspCdfFileName != null
                && normalStyCelFileName != null && tumorStyCelFileName != null
                && styCdfFileName != null)) {
                jButtonStartAnalysis.setEnabled(true);

        }


    }//GEN-LAST:event_jButtonTumorCelFileChooser1ActionPerformed

    private void jButtonCdfFileChooser1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCdfFileChooser1ActionPerformed
        // TODO add your handling code here:

        int returnVal = jFileChooserCdfFile.showOpenDialog(mainPanel);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = jFileChooserCdfFile.getSelectedFile();
                //This is where a real application would save the file.
                jTextFieldCdfFile1.setText(jFileChooserCelFile.getName(file));
                if (chipType.equals("Affymetrix GeneChip Human Mapping 500K Array Set")) {
                    nspCdfFileName = file.getPath();
                } else {
                    cdfFileName = file.getPath();
                }

            } else {

            }


        if ((normalCelFileName != null && tumorCelFileName != null && cdfFileName != null) ||
                (normalNspCelFileName != null && tumorNspCelFileName != null && nspCdfFileName != null
                && normalStyCelFileName != null && tumorStyCelFileName != null
                && styCdfFileName != null)) {
                jButtonStartAnalysis.setEnabled(true);

        }


    }//GEN-LAST:event_jButtonCdfFileChooser1ActionPerformed

    private void jButtonCdfFileChooser2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCdfFileChooser2ActionPerformed
        // TODO add your handling code here:
        int returnVal = jFileChooserCdfFile.showOpenDialog(mainPanel);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = jFileChooserCdfFile.getSelectedFile();
                //This is where a real application would save the file.
                jTextFieldCdfFile2.setText(jFileChooserCelFile.getName(file));
                if (chipType.equals("Affymetrix GeneChip Human Mapping 500K Array Set")) {
                    styCdfFileName = file.getPath();
                }

            } else {

            }


        if ((normalCelFileName != null && tumorCelFileName != null && cdfFileName != null) ||
                (normalNspCelFileName != null && tumorNspCelFileName != null && nspCdfFileName != null
                && normalStyCelFileName != null && tumorStyCelFileName != null
                && styCdfFileName != null)) {
                jButtonStartAnalysis.setEnabled(true);

        }
    }//GEN-LAST:event_jButtonCdfFileChooser2ActionPerformed

    private void jButtonNormalCelFileChooser2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNormalCelFileChooser2ActionPerformed
        // TODO add your handling code here:
        int returnVal = jFileChooserCelFile.showOpenDialog(mainPanel);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = jFileChooserCelFile.getSelectedFile();
                //This is where a real application would save the file.
                jTextFieldNormalCelFile2.setText(jFileChooserCelFile.getName(file));
                if (chipType.equals("Affymetrix GeneChip Human Mapping 500K Array Set")) {
                    normalStyCelFileName = file.getPath();
                } 

            } else {

            }


        if ((normalCelFileName != null && tumorCelFileName != null && cdfFileName != null) ||
                (normalNspCelFileName != null && tumorNspCelFileName != null && nspCdfFileName != null
                && normalStyCelFileName != null && tumorStyCelFileName != null
                && styCdfFileName != null)) {
                jButtonStartAnalysis.setEnabled(true);

        }

    }//GEN-LAST:event_jButtonNormalCelFileChooser2ActionPerformed

    private void jButtonTumorCelFileChooser2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonTumorCelFileChooser2ActionPerformed
        // TODO add your handling code here:
        int returnVal = jFileChooserCelFile.showOpenDialog(mainPanel);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = jFileChooserCelFile.getSelectedFile();
                //This is where a real application would save the file.
                jTextFieldTumorCelFile2.setText(jFileChooserCelFile.getName(file));
                if (chipType.equals("Affymetrix GeneChip Human Mapping 500K Array Set")) {
                    tumorStyCelFileName = file.getPath();
                }

            } else {

            }

        if ((normalCelFileName != null && tumorCelFileName != null && cdfFileName != null) ||
                (normalNspCelFileName != null && tumorNspCelFileName != null && nspCdfFileName != null
                && normalStyCelFileName != null && tumorStyCelFileName != null
                && styCdfFileName != null)) {
                jButtonStartAnalysis.setEnabled(true);

        }

    }//GEN-LAST:event_jButtonTumorCelFileChooser2ActionPerformed

    private void jButtonStartAnalysisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStartAnalysisActionPerformed
        // TODO add your handling code here:
        
        if ((normalCelFileName != null && tumorCelFileName != null && cdfFileName != null) || 
                (normalNspCelFileName != null && tumorNspCelFileName != null && nspCdfFileName != null
                && normalStyCelFileName != null && tumorStyCelFileName != null 
                && styCdfFileName != null)) {

         
                jTextFieldNormalCelFile1.setEnabled(false);
                jTextFieldNormalCelFile2.setEnabled(false);
                jTextFieldTumorCelFile1.setEnabled(false);
                jTextFieldTumorCelFile2.setEnabled(false);

                jButtonNormalCelFileChooser1.setEnabled(false);
                jButtonNormalCelFileChooser2.setEnabled(false);
                jButtonTumorCelFileChooser1.setEnabled(false);
                jButtonTumorCelFileChooser2.setEnabled(false);

                jButtonCdfFileChooser1.setEnabled(false);
                jButtonCdfFileChooser2.setEnabled(false);
                jTextFieldCdfFile1.setEnabled(false);
                jTextFieldCdfFile2.setEnabled(false);

                jButtonStartAnalysis.setEnabled(false);




                Task task = new Task();
                task.execute();
            
                
                
            if (chipType.equals("Affymetrix GeneChip Human Mapping 500K Array Set")) {

                jTextFieldNormalCelFile1.setEnabled(true);
                jTextFieldNormalCelFile2.setEnabled(true);
                jTextFieldTumorCelFile1.setEnabled(true);
                jTextFieldTumorCelFile2.setEnabled(true);

                jButtonNormalCelFileChooser1.setEnabled(true);
                jButtonNormalCelFileChooser2.setEnabled(true);
                jButtonTumorCelFileChooser1.setEnabled(true);
                jButtonTumorCelFileChooser2.setEnabled(true);

                jButtonCdfFileChooser1.setEnabled(true);
                jButtonCdfFileChooser2.setEnabled(true);
                jTextFieldCdfFile1.setEnabled(true);
                jTextFieldCdfFile2.setEnabled(true);



            } else {
                jButtonCdfFileChooser1.setEnabled(true);
                jButtonCdfFileChooser2.setEnabled(false);
                jTextFieldCdfFile1.setEnabled(true);
                jTextFieldCdfFile2.setEnabled(false);

                jTextFieldNormalCelFile1.setEnabled(true);
                jTextFieldNormalCelFile2.setEnabled(false);
                jTextFieldTumorCelFile1.setEnabled(true);
                jTextFieldTumorCelFile2.setEnabled(false);

                jButtonNormalCelFileChooser1.setEnabled(true);
                jButtonNormalCelFileChooser2.setEnabled(false);
                jButtonTumorCelFileChooser1.setEnabled(true);
                jButtonTumorCelFileChooser2.setEnabled(false);

            }

                jButtonStartAnalysis.setEnabled(true);



            }


    }//GEN-LAST:event_jButtonStartAnalysisActionPerformed

    private void jTextFieldNormalCelFile1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextFieldNormalCelFile1MouseClicked
        // TODO add your handling code here:
        int returnVal = jFileChooserCelFile.showOpenDialog(mainPanel);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = jFileChooserCelFile.getSelectedFile();
                //This is where a real application would save the file.
                jTextFieldNormalCelFile1.setText(jFileChooserCelFile.getName(file));
                if (chipType.equals("Affymetrix GeneChip Human Mapping 500K Array Set")) {
                    normalNspCelFileName = file.getPath();
                } else {
                    normalCelFileName = file.getPath();
                }

            } else {

            }

        if ((normalCelFileName != null && tumorCelFileName != null && cdfFileName != null) ||
                (normalNspCelFileName != null && tumorNspCelFileName != null && nspCdfFileName != null
                && normalStyCelFileName != null && tumorStyCelFileName != null
                && styCdfFileName != null)) {
                jButtonStartAnalysis.setEnabled(true);

        }
    }//GEN-LAST:event_jTextFieldNormalCelFile1MouseClicked

    private void jTextFieldNormalCelFile2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextFieldNormalCelFile2MouseClicked
        // TODO add your handling code here:
        if (chipType.equals("Affymetrix GeneChip Human Mapping 500K Array Set")) {
            int returnVal = jFileChooserCelFile.showOpenDialog(mainPanel);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = jFileChooserCelFile.getSelectedFile();
                //This is where a real application would save the file.
                jTextFieldNormalCelFile2.setText(jFileChooserCelFile.getName(file));
                if (chipType.equals("Affymetrix GeneChip Human Mapping 500K Array Set")) {
                    normalStyCelFileName = file.getPath();
                }

            } else {
            }


            if ((normalCelFileName != null && tumorCelFileName != null && cdfFileName != null)
                    || (normalNspCelFileName != null && tumorNspCelFileName != null && nspCdfFileName != null
                    && normalStyCelFileName != null && tumorStyCelFileName != null
                    && styCdfFileName != null)) {
                jButtonStartAnalysis.setEnabled(true);

            }


        }

    }//GEN-LAST:event_jTextFieldNormalCelFile2MouseClicked

    private void jTextFieldTumorCelFile1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextFieldTumorCelFile1MouseClicked
        // TODO add your handling code here:

        int returnVal = jFileChooserCelFile.showOpenDialog(mainPanel);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = jFileChooserCelFile.getSelectedFile();
                //This is where a real application would save the file.
                jTextFieldTumorCelFile1.setText(jFileChooserCelFile.getName(file));
                if (chipType.equals("Affymetrix GeneChip Human Mapping 500K Array Set")) {
                    tumorNspCelFileName = file.getPath();
                } else {
                    tumorCelFileName = file.getPath();
                }

            } else {

            }


        if ((normalCelFileName != null && tumorCelFileName != null && cdfFileName != null) ||
                (normalNspCelFileName != null && tumorNspCelFileName != null && nspCdfFileName != null
                && normalStyCelFileName != null && tumorStyCelFileName != null
                && styCdfFileName != null)) {
                jButtonStartAnalysis.setEnabled(true);

        }
    }//GEN-LAST:event_jTextFieldTumorCelFile1MouseClicked

    private void jTextFieldTumorCelFile2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextFieldTumorCelFile2MouseClicked
        // TODO add your handling code here:
        if (chipType.equals("Affymetrix GeneChip Human Mapping 500K Array Set")) {
            int returnVal = jFileChooserCelFile.showOpenDialog(mainPanel);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = jFileChooserCelFile.getSelectedFile();
                //This is where a real application would save the file.
                jTextFieldTumorCelFile2.setText(jFileChooserCelFile.getName(file));
                if (chipType.equals("Affymetrix GeneChip Human Mapping 500K Array Set")) {
                    tumorStyCelFileName = file.getPath();
                }

            } else {
            }

            if ((normalCelFileName != null && tumorCelFileName != null && cdfFileName != null)
                    || (normalNspCelFileName != null && tumorNspCelFileName != null && nspCdfFileName != null
                    && normalStyCelFileName != null && tumorStyCelFileName != null
                    && styCdfFileName != null)) {
                jButtonStartAnalysis.setEnabled(true);

            }
        }

    }//GEN-LAST:event_jTextFieldTumorCelFile2MouseClicked

    private void jTextFieldCdfFile1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextFieldCdfFile1MouseClicked
        // TODO add your handling code here:
        int returnVal = jFileChooserCdfFile.showOpenDialog(mainPanel);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = jFileChooserCdfFile.getSelectedFile();
                //This is where a real application would save the file.
                jTextFieldCdfFile1.setText(jFileChooserCelFile.getName(file));
                if (chipType.equals("Affymetrix GeneChip Human Mapping 500K Array Set")) {
                    nspCdfFileName = file.getPath();
                } else {
                    cdfFileName = file.getPath();
                }

            } else {

            }


        if ((normalCelFileName != null && tumorCelFileName != null && cdfFileName != null) ||
                (normalNspCelFileName != null && tumorNspCelFileName != null && nspCdfFileName != null
                && normalStyCelFileName != null && tumorStyCelFileName != null
                && styCdfFileName != null)) {
                jButtonStartAnalysis.setEnabled(true);

        }
    }//GEN-LAST:event_jTextFieldCdfFile1MouseClicked

    private void jTextFieldCdfFile2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextFieldCdfFile2MouseClicked
        // TODO add your handling code here:
        if (chipType.equals("Affymetrix GeneChip Human Mapping 500K Array Set")) {
            int returnVal = jFileChooserCdfFile.showOpenDialog(mainPanel);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = jFileChooserCdfFile.getSelectedFile();
                //This is where a real application would save the file.
                jTextFieldCdfFile2.setText(jFileChooserCelFile.getName(file));
                if (chipType.equals("Affymetrix GeneChip Human Mapping 500K Array Set")) {
                    styCdfFileName = file.getPath();
                }

            } else {
            }


            if ((normalCelFileName != null && tumorCelFileName != null && cdfFileName != null)
                    || (normalNspCelFileName != null && tumorNspCelFileName != null && nspCdfFileName != null
                    && normalStyCelFileName != null && tumorStyCelFileName != null
                    && styCdfFileName != null)) {
                jButtonStartAnalysis.setEnabled(true);

            }
        }

    }//GEN-LAST:event_jTextFieldCdfFile2MouseClicked



    class Task extends SwingWorker<Void, Void> {
        /*
         * Main task. Executed in background thread.
         */
        @Override
        public Void doInBackground() {
            String resultMessage = "\n  Ananlysis begins ... \n\n  Reading CEL files...\n\n";
            jTextAreaResult.setText(resultMessage);
            if (chipType.equals("Affymetrix GeneChip Human Mapping 500K Array Set")) {
                 cnv = new AffyCopyNumberData(normalNspCelFileName, normalStyCelFileName, tumorNspCelFileName,
                                tumorStyCelFileName, nspCdfFileName, styCdfFileName);
            } else {
                cnv = new AffyCopyNumberData(normalCelFileName, tumorCelFileName, cdfFileName);
            }


            if (cnv.isSuccessfulOpenFile && cnv.isSuccessfulOpenAnnotationFile) {

                cnv.localNormalization();

                jTextAreaResult.append("  Finished reading CEL files.\n\n");
                jTextAreaResult.append("  Begin copy number detection ...\n\n");

                cnvDetection = new CopyNumberDetection(cnv);

                jTextAreaResult.append("  Copy number detection is done.\n\n");

                D = new Noticcor(cnv, cnvDetection);

                D.correctNTC();

                jTextAreaResult.append("  BACOM is done.\n\n");

//                cnnloh = new CopyNeutralLOH(cnv, cnvDetection);

                jTextAreaResult.append("  Results are being saved.\n\n");

                try {
                     writeResults();
                } catch (Exception e) {

                }

            } else {
                if (!cnv.isSuccessfulOpenFile) {
                    jTextAreaResult.setText("\n  Errors in reading CEL/CDF files." +
                             "\n  Please check input files.\n"
                            + "\n  The input normal cel file:\t" + normalCelFileName
                            + "\n  The input tumor cel file:\t" + tumorCelFileName
                            + "\n  The input cdf file:\t" + cdfFileName);
                } else if (!cnv.isSuccessfulOpenAnnotationFile) {
                    jTextAreaResult.setText("\n  Error in reading annotation files."
                            + "\n  Please make sure correct annotation files "
                            + "are placed under folder annotation.\n");
                }
                 
            }


            return null;

            
        }

        /*
         * Executed in event dispatching thread
         */
        @Override
        public void done() {

        }
    }


    private void writeResults_test() throws IOException {

        BufferedWriter resultFile = null;

        try {
            resultFile = new BufferedWriter(new FileWriter("results" + File.separator + "data.csv"), 30 * 1024 * 1024);

            for (int i = 0; i < cnv.probeSetID.length; i++) {

                if (cnv.isOutlier[i]) {
                    String entry = i + cnv.chrID[i] + ","
                            + cnv.copyNumber[i] + "," + cnv.alleleA[i] + ","
                            + cnv.alleleB[i];

                    resultFile.write(entry.toCharArray());
                    resultFile.newLine();
                }

            }
        } finally {
            if (resultFile != null) {
                resultFile.close();
            }
        }

        jTextAreaResult.append("  Analysis is complete. Test results are saved under folder results.\n\n");

    }




    private void writeResults() throws IOException {


        

	BufferedWriter resultFile = null;

        double[] segMean = new double[cnv.probeSetID.length];

        int k = 0;
        for (int i = 0; i < cnvDetection.chromosome.size(); i++) {

            for (int j = cnvDetection.segStartPos.get(i); j <= cnvDetection.segEndPos.get(i); j++) {
                segMean[k] = cnvDetection.segStatus.get(i);
                k ++;
            }

        }


        try {

            if (!(new File("results")).exists()) {
                boolean isSucessful = (new File("results")).mkdir();
            }

            resultFile = new BufferedWriter(new FileWriter("results" + File.separator
                    + "data.csv"), 30 * 1024 * 1024);

	    for (int i = 0; i < cnv.probeSetID.length; i ++) {
		String entry = cnv.probeSetID[i] + "," + cnv.chrID[i] + "," + cnv.location[i] + ","
                        + cnv.copyNumber[i] + "," + segMean[i];
//		String entry = cnv.probeSetID[i] + "," + cnv.chrID[i] + "," + cnv.location[i] + ","
//                        + cnv.copyNumber[i] + "," +cnv.alleleA[i] + "," + cnv.alleleB[i] + ","
//                        + (cnv.isGenotypeAB[i]? 1:0);
//
                resultFile.write(entry.toCharArray());
		resultFile.newLine();
            }
	} finally {
	    if (resultFile != null) {
                resultFile.close();
            }
        }



//	BufferedWriter detectionResultFile = null;
//
//        try {
//            detectionResultFile = new BufferedWriter(new FileWriter("results"
//                    + File.separator + "detectionResult.csv"), 30 * 1024 * 1024);
//
//	    for (int i = 0; i < cnvDetection.chromosome.size(); i ++) {
//		String entry = cnvDetection.chromosome.get(i) + ","
//		    + cnvDetection.segStartPos.get(i) + ","
//		    + cnvDetection.segEndPos.get(i) + ","
//		    + cnvDetection.segStatus.get(i);
//		detectionResultFile.write(entry.toCharArray());
//		detectionResultFile.newLine();
//            }
//	} finally {
//	    if (detectionResultFile != null) {
//                detectionResultFile.close();
//            }
//        }



        BufferedWriter alphaFile = null;

        try {
            alphaFile = new BufferedWriter(new FileWriter("results"
                    + File.separator + "alpha.txt"), 30 * 1024 * 1024);

		String entry = "" + D.alpha;
		alphaFile.write(entry.toCharArray());
                alphaFile.newLine();
	} finally {
	    if (alphaFile != null) {
                alphaFile.close();
            }
        }



	BufferedWriter correctionResultFile = null;

        try {
            correctionResultFile = new BufferedWriter(new FileWriter("results" + File.separator + "BACOMresults.txt"), 30*1024*1024);

	    for (int i = 0; i < D.deletionSegChrID.size(); i ++) {

		String entry = "Chr " + D.deletionSegChrID.get(i) + ":    from "
		    + D.deletionSegStartLocation.get(i)+ " to " + D.deletionSegEndLocation.get(i) + "\n     Pr of hemi="
		    + D.deletionSegProbHemiDeletion.get(i) + "\n     Pr of homo="
		    + D.deletionSegProbHomoDeletion.get(i) + "\n     Normal tissue fraction="
		    + D.deletionSegNormalTissueFraction.get(i) + "\n";

		correctionResultFile.write(entry.toCharArray());
		correctionResultFile.newLine();

            }

	    String entry = "The average normal tissue contamination fraction is " + D.alpha;
	    correctionResultFile.write(entry.toCharArray());
	    correctionResultFile.newLine();


	} finally {
	    if (correctionResultFile != null) {
                correctionResultFile.close();
            }
        }



        /*Write detection results with physical locations on the chromosomes   */

//	detectionResultFile = null;
//
//        try {
//            detectionResultFile = new BufferedWriter(new FileWriter("results/CNVsegments.csv"), 30*1024*1024);
//
//	    for (int i = 0; i < cnvDetection.chromosomeStartIndex.size(); i ++) {
//		int chrStart = cnvDetection.chromosomeStartIndex.get(i);
//                String entry = null;
//                for (int j = 0; j < cnvDetection.chromosome.size(); j ++) {
//                    if (cnv.chrID[chrStart].equals(cnvDetection.chromosome.get(j))) {
//                        entry = cnvDetection.chromosome.get(j) + ","
//                            + cnv.location[chrStart+cnvDetection.segStartPos.get(j)] + ","
//                            + cnv.location[chrStart+cnvDetection.segEndPos.get(j)] + ","
//                            + cnvDetection.segStatus.get(j);
//                        detectionResultFile.write(entry.toCharArray());
//                        detectionResultFile.newLine();
//                    }
//                }
//
//            }
//	} finally {
//	    if (detectionResultFile != null) {
//                detectionResultFile.close();
//            }
//        }


        /*Write BACOM results with physical locations on the chromosomes   */

	correctionResultFile = null;

        try {
            correctionResultFile = new BufferedWriter(new FileWriter("results" + File.separator + "BACOM_locations.txt"), 30*1024*1024);

	    for (int i = 0; i < D.deletionSegChrID.size(); i ++) {

		String entry = "Chr " + D.deletionSegChrID.get(i) + ":    from "
		    + cnv.location[D.deletionSegStartLocation.get(i)]+ " to "
                    + cnv.location[D.deletionSegEndLocation.get(i)] + "\n     Pr of hemi-deletion="
		    + D.deletionSegProbHemiDeletion.get(i) + "\n     Pr of homo-deletion="
		    + D.deletionSegProbHomoDeletion.get(i) + "\n     Normal tissue fraction="
		    + D.deletionSegNormalTissueFraction.get(i) + "\n";

		correctionResultFile.write(entry.toCharArray());
		correctionResultFile.newLine();

            }

	    String entry = "The average normal tissue contamination fraction is " + D.alpha;
	    correctionResultFile.write(entry.toCharArray());
	    correctionResultFile.newLine();


	} finally {
	    if (correctionResultFile != null) {
                correctionResultFile.close();
            }
        }


//	BufferedWriter cnnlohResultFile = null;
//
//        try {
//            cnnlohResultFile = new BufferedWriter(new FileWriter("results" + File.separator + "cnnloh.csv"), 30*1024*1024);
//
//	    for (int i = 0; i < cnnloh.chrID.length; i ++) {
//
//		String entry = cnnloh.chrID[i] + "," + cnnloh.location[i] + "," + cnnloh.indexOnChr[i] + ","
//		    + cnnloh.alleleA[i] + "," + cnnloh.alleleB[i] + "," + cnnloh.copyNumber[i] + ","
//		    + (cnnloh.isGenotypeAB[i]? 1:0);
//		cnnlohResultFile.write(entry.toCharArray());
//		cnnlohResultFile.newLine();
//
//            }
//
//
//	} finally {
//	    if (cnnlohResultFile != null) {
//                cnnlohResultFile.close();
//            }
//        }
//
//
//
//        try {
//            cnnlohResultFile = new BufferedWriter(new FileWriter("results/cnnlohResult.txt"), 30*1024*1024);
//
//	    for (int i = 0; i < cnnloh.chromosome.size(); i ++) {
//
//		String entry = cnnloh.chromosome.get(i) + "," + cnnloh.cnnlohSegStartLocation.get(i) + ","
//		    + cnnloh.cnnlohSegEndLocation.get(i) + "," + cnnloh.cnnlohSegStartIndex.get(i) + ","
//		    + cnnloh.cnnlohSegEndIndex.get(i);
//		cnnlohResultFile.write(entry.toCharArray());
//		cnnlohResultFile.newLine();
//
//            }
//
//
//	} finally {
//	    if (cnnlohResultFile != null) {
//                cnnlohResultFile.close();
//            }
//        }
//


        jTextAreaResult.append("  Analysis is complete. Results are saved under folder results.\n\n");

        DecimalFormat alphaFormatter = new DecimalFormat("##.#%");

        jTextAreaResult.append("  The average normal tissue contamination fraction in this sample is " 
                + alphaFormatter.format(D.alpha) + ".");
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCdfFileChooser1;
    private javax.swing.JButton jButtonCdfFileChooser2;
    private javax.swing.JButton jButtonNormalCelFileChooser1;
    private javax.swing.JButton jButtonNormalCelFileChooser2;
    private javax.swing.JButton jButtonStartAnalysis;
    private javax.swing.JButton jButtonTumorCelFileChooser1;
    private javax.swing.JButton jButtonTumorCelFileChooser2;
    private javax.swing.JComboBox jComboBoxChipType;
    private javax.swing.JFileChooser jFileChooserCdfFile;
    private javax.swing.JFileChooser jFileChooserCelFile;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabelCdf1;
    private javax.swing.JLabel jLabelCdf2;
    private javax.swing.JLabel jLabelNormalCel1;
    private javax.swing.JLabel jLabelNormalCel2;
    private javax.swing.JLabel jLabelTumorCel1;
    private javax.swing.JLabel jLabelTumorCel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextArea jTextAreaResult;
    private javax.swing.JTextField jTextFieldCdfFile1;
    private javax.swing.JTextField jTextFieldCdfFile2;
    private javax.swing.JTextField jTextFieldNormalCelFile1;
    private javax.swing.JTextField jTextFieldNormalCelFile2;
    private javax.swing.JTextField jTextFieldTumorCelFile1;
    private javax.swing.JTextField jTextFieldTumorCelFile2;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;

    private String normalNspCelFileName = null;
    private String tumorNspCelFileName = null;
    private String nspCdfFileName = null;
    private String normalStyCelFileName = null;
    private String tumorStyCelFileName = null;
    private String styCdfFileName = null;


    private String normalCelFileName = null;
    private String tumorCelFileName = null;
    private String cdfFileName = null;

    private String chipType;

    private AffyCopyNumberData cnv;
    private CopyNumberDetection cnvDetection;
    private Noticcor D;
//    private CopyNeutralLOH cnnloh;


}
